package com.aidansmeaton.weightmanager;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.aidansmeaton.weightmanager.data.DatabaseHelper;
import com.aidansmeaton.weightmanager.util.TimeFormatter;
import com.quietlycoding.android.picker.NumberPicker;
import com.quietlycoding.android.picker.NumberPicker.OnChangedListener;

/** The first activity in the app. */
public class SetUpPersonalActivity extends Activity {
	
	private final int POUNDS_IN_STONE = 14;
	private final int MAX_POUNDS = 13;
	private final int MAX_STONES = 99;
	private final int MIN_POUNDS = 0;
	private final int MIN_STONES = 0;
	
	private DatabaseHelper db;
	private Calendar c;
	
	// Personal
	private EditText nameEdit;
	private EditText ageEdit;
	private NumberPicker stonesPicker;
	private NumberPicker poundsPicker;
	private RadioButton femaleRadio;
	private RadioButton maleRadio;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	setContentView(R.layout.setup_personal);
        
        // Stop keyboard popping up automatically
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     
        
        setTitle("Setup - Personal Info");
        db = DatabaseHelper.getInstance(this);
        c = Calendar.getInstance();
		
		nameEdit = (EditText) findViewById(R.id.nameEdit);
		ageEdit = (EditText) findViewById(R.id.ageEdit);
        stonesPicker = (NumberPicker) findViewById(R.id.stones_picker);
        poundsPicker = (NumberPicker) findViewById(R.id.pounds_picker);
        femaleRadio = (RadioButton) findViewById(R.id.female);
        maleRadio = (RadioButton) findViewById(R.id.male);
        
        nameEdit.setText(getSharedPreferences("Profile", 0).getString("Name", ""));
        if (getSharedPreferences("Profile", 0).getInt("Age",-1) != -1) ageEdit.setText(""+getSharedPreferences("Profile", 0).getInt("Age",-1));
        femaleRadio.setChecked(getSharedPreferences("Profile", 0).getBoolean("Gender",true));
        maleRadio.setChecked(!femaleRadio.isChecked());
        
        /*
        stonesPicker.setRange(MIN_STONES, MAX_STONES);
        poundsPicker.setRange(MIN_POUNDS, MAX_POUNDS);        

        Cursor c = db.getWeightLogs();
        if (c.getCount() > 0){
        	c.moveToFirst();
        	int weight = c.getInt(DatabaseHelper.WEIGHT_LOG_WEIGHT);
        	stonesPicker.setCurrent(weight/POUNDS_IN_STONE);
            poundsPicker.setCurrent(weight%POUNDS_IN_STONE);
        }
        else {
        	stonesPicker.setCurrent(0);
            poundsPicker.setCurrent(0);
        }
        */
        
    }
    
	private boolean setUpPersonal(){
		
		boolean success = true;
		String name;
		int age;
		boolean isFemale = femaleRadio.isChecked();
		
		try {
			// try this
			name = nameEdit.getText().toString();
			age = Integer.parseInt(ageEdit.getText().toString());
			//weight = (stonesPicker.getCurrent()*POUNDS_IN_STONE)+poundsPicker.getCurrent();
			if (name.length() < 1) throw (new NumberFormatException("Invalid name"));
			if (age < 18) throw (new NumberFormatException("Invalid age"));
			
			// success
			getSharedPreferences("Profile", 0).edit().putString("Name", name).commit();
			getSharedPreferences("Profile", 0).edit().putInt("Age", age).commit();
			getSharedPreferences("Profile", 0).edit().putBoolean("Gender", isFemale).commit();
			/*if (getSharedPreferences("Profile", 0).getInt("StartWeight",-1) == -1){
				getSharedPreferences("Profile", 0).edit().putInt("StartWeight", weight).commit();
				getSharedPreferences("Profile", 0).edit().putString("StartDate", TimeFormatter.getDatabaseDateString(Calendar.getInstance())).commit();
			}
			db.editWeightLog(TimeFormatter.getDatabaseDateString(c), weight, "");*/
			
		} catch (NumberFormatException e){
			success = false;
			if (e.getMessage().equals("unable to parse '' as integer") || e.getMessage().equals("Invalid age")){
				Toast.makeText(this, "Please enter an age over 18.", Toast.LENGTH_SHORT).show();
			}
			else if (e.getMessage().equals("Invalid name")){
				Toast.makeText(this, "Please enter your name.", Toast.LENGTH_SHORT).show();
			}
		}
		
		return success;
	}
	
	public void nextClick(View v){
		if (setUpPersonal()){
			finish();
			Intent setupIntent = new Intent(this, SetUpExerciseActivity.class);
			this.startActivity(setupIntent);
		}
	}
	
	public void laterClick(View v){
		finish();
	}
	
}